package net.maku.kyy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.kyy.convert.KyyReportConvert;
import net.maku.kyy.entity.KyyReportEntity;
import net.maku.kyy.query.KyyReportQuery;
import net.maku.kyy.vo.KyyReportVO;
import net.maku.kyy.dao.KyyReportDao;
import net.maku.kyy.service.KyyReportService;
import com.fhs.trans.service.impl.TransService;
import net.maku.framework.common.utils.ExcelUtils;
import net.maku.kyy.vo.KyyReportExcelVO;
import net.maku.framework.common.excel.ExcelFinishCallBack;
import org.springframework.web.multipart.MultipartFile;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 举报信息
 *
 * @author 智 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@Service
@AllArgsConstructor
public class KyyReportServiceImpl extends BaseServiceImpl<KyyReportDao, KyyReportEntity> implements KyyReportService {
    private final TransService transService;

    @Override
    public PageResult<KyyReportVO> page(KyyReportQuery query) {
        IPage<KyyReportEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(KyyReportConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }


    private LambdaQueryWrapper<KyyReportEntity> getWrapper(KyyReportQuery query){
        LambdaQueryWrapper<KyyReportEntity> wrapper = Wrappers.lambdaQuery();

        return wrapper;
    }


    @Override
    public KyyReportVO get(Long id) {
        KyyReportEntity entity = baseMapper.selectById(id);
        KyyReportVO vo = KyyReportConvert.INSTANCE.convert(entity);

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(KyyReportVO vo) {
        KyyReportEntity entity = KyyReportConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(KyyReportVO vo) {
        KyyReportEntity entity = KyyReportConvert.INSTANCE.convert(vo);

        updateById(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);

    }



    @Override
    public void export() {
    List<KyyReportExcelVO> excelList = KyyReportConvert.INSTANCE.convertExcelList(list());
        transService.transBatch(excelList);
        ExcelUtils.excelExport(KyyReportExcelVO.class, "举报信息", null, excelList);
    }


    @Override
    public boolean InsertReport(KyyReportEntity kyyReportEntity){
        return save(kyyReportEntity);
    }

    @Override
    public List<KyyReportEntity> SelectReport(KyyReportEntity kyyReportEntity){
        QueryWrapper queryWrapper = new QueryWrapper();
        if(kyyReportEntity.getId() != null) queryWrapper.like("id",kyyReportEntity.getId());
        if(kyyReportEntity.getPinglunId() != null) queryWrapper.like("pinglun_id",kyyReportEntity.getPinglunId());
        if(kyyReportEntity.getForumId() != null) queryWrapper.like("forum_id",kyyReportEntity.getForumId());
        if(kyyReportEntity.getTieId() != null) queryWrapper.like("tie_id",kyyReportEntity.getTieId());
        if(kyyReportEntity.getReporttype() != null) queryWrapper.like("reporttype",kyyReportEntity.getReporttype());
        if(kyyReportEntity.getContactsphone() != null) queryWrapper.like("contactsphone",kyyReportEntity.getContactsphone());
        return list(queryWrapper);
    }

}